
const express = require('express');
const router = express.Router();
const sqlFn = require('./mysql');

const Mock = require('mockjs');


// 订单列表 
/*
router.get('/list',(req,res)=>{
  const page = req.query.page || 1;
  const sqlLen = "select * from orderinfo where id";
  sqlFn(sqlLen,null,(data)=>{
     const len = data.length;
     const sql = "select * from orderinfo order by id desc limit 8 offset" + (page-1) *8;
     sqlFn(sql,null,(result)=>{
       if(result.length>0){
          res.send({
            status:200,
            data:result,
            pageSize:8,
            total:len,
          })
       }else{
          res.send({
            status:500,
            msg:'订单列表获取失败'
          })
       }
     })
  })
}) */


// 订单列表 
router.get('/list',(req,res)=>{
   res.send(Mock.mock({
      info:'数据统计',
      success:true,
      data:{
         "list|14":[{
            "id":17,
            "code":"124563956",
            "ordername":"测试数据",
            "company":"测试公司",
            "phone":"151" + ~~(Math.random()*1000000) + ~~(Math.random()*100),
            "yudingTime":'2022-12-14T16:00:00.000Z',
            "price":"230",
            "huizongStatus|1-2":1,
         }]
      },
      pageSize:8,
      status:200,
      total:14
   }))
})




/*
// 汇总状态改变
router.get('/changeStatus',(req,res)=>{
   let ids = req.query.ids;
   const sql = `update orderinfo set huizongStatus='2' where id in (${ids})`;
   sqlFn(sql,null,(result)=>{
      if(result.affectedRows > 0){
        res.send({
            status:200,
            msg:'修改成功'
        })
        const sqlLen = `select * from orderinfo where id in ((${ids})`;
        sqlFn(sqlLen,null,(data)=>{
           let sum = 0;
           data.forEach(ele=>{
               sum += Number(ele.price)
           })
           let code = ~~(Math.random() * 1000000);
           let phone = "151" + ~~(Math.random()*1000000) + ~~(Math.random()*100);
           const sql = `insert into collect(orderNum,phone,totalPrice,ids,operator) values(${code},${phone},?,?,'张三')`;
           let arr = [sum,ids];
           sqlFn(sql,arr,(result)=>{
           })
        })
      }else{
         res.send({
            status:500,
            msg:'修改失败'
          })
      }
   })
})  */




// 汇总状态改变
router.get('/changeStatus',(req,res)=>{
    var id = req.query.id;
    const sql = "update from project where id=?"
    const arr = [id];
    sqlFn(sql,arr,result=>{
       if(result.affectedRows > 0){
          res.send({
             status:200,
             msg:'汇总成功'
          })
       }else{
         res.send({
            status:500,
            msg:'汇总失办'
         })
       }
    })
})





// 订单详情 
var MockRandom = Mock.Random;
router.get('/detail',(req,res)=>{
     let arr = ["猪肉","鸡肉","牛肉","羊肉","苹果","莲藕","青菜","白菜","香蕉","柚子","西瓜"]
     res.send(Mock.mock({
      info:'订单详情',
      success:true,
      data:{
         "list|10":[
            {
            "title|+1":arr,
            "price|10-100":1,
            "num|1-1000":1,
            "guige|1-200":3,
            "sum|1-500":230
            }]
      },
      code:"3565786",
      created:MockRandom.now("yyyy-MM-dd HH:mm:ss"),
      yudingTime:MockRandom.now("yyyy-MM-dd HH:mm:ss"),
   }))
})










// 汇总清单列表
/*
router.get('/collect',(req,res)=>{
   const page = req.query.page || 1;
   const sqlLen = "select * from collect";
   sqlFn(sqlLen,null,(data)=>{
      let len = data.length;
      const sql = "select * from collect order by id desc limit 8 offset" + (page-1) * 8;
      sqlFn(sql,null,(result)=>{
         if(result.length > 0){
             res.send({
                status:200,
                data:result
             })
         }else{


         }
      })
   })
}) */
 

// 汇总清单列表
router.get('/collect',(req,res)=>{
   res.send(Mock.mock({
      info:'数据统计',
      success:true,
      data:{
         "list|3":[{
            "id":33,
            "ids":"9,8",
            "operator":"张三",
            "orderNum":611129,
            "phone":"151" + ~~(Math.random()*1000000) + ~~(Math.random()*100),
            "time":'2023-12-14T05:38:43.000Z',
            "totalPrice":500,
         }]
      },
      pageSize:8,
      status:200,
      total:14
   }))
})




// 撤销汇总 
router.get('/cancel',(req,res)=>{
   const id = req.query.id;
   const sql = `select ids from collect where id=${id}`;
   sqlFn(sql,null,(arr)=>{
      const ids = arr[0].ids;
      const sqldel = `delete from collect where id=${id}`;
      sqlFn(sqldel,null,result=>{
          const sql = `update orderinfo set huizongStatus='1' where id in (${ids})`;
          sqlFn(sql,null,data=>{
             if(data.affectedRows > 0){
               res.send({
                  status:200,
                  msg:'撤销成功'
               })
             }
         })
      })
   })
})






module.exports = router;